package com.opengms.HydroModelsIntegration.Dao;

import net.sf.json.JSONObject;
import sun.net.www.http.HttpClient;

import java.io.*;
import java.net.*;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: David.Xiao
 * @Date: 2019/1/14 21:12
 * @Description:
 */
public class UrlHelper {

    private String urlStr;
    private URL url;
    private HttpURLConnection conn;

    public UrlHelper(String urlStr)
    {
        this.urlStr = urlStr;
        try {
            this.url = new URL(urlStr);
            this.conn = (HttpURLConnection)url.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 从UrlConnection中获取文件名称
    public  String getFileName() {
        String filename = "";
        boolean isok = false;
        if (conn == null) {
            return null;
        }
        String ss = conn.getHeaderField("Content-Disposition");
        Map<String, List<String>> hf = conn.getHeaderFields();
        if (hf == null) {
            return null;
        }
        Set<String> key = hf.keySet();
        if (key == null) {
            return null;
        }
        for (String skey : key) {
            List<String> values = hf.get(skey);
            for (String value : values) {
                String result;
                try {
                    result = new String(value.getBytes("ISO-8859-1"), "GBK");
                    int location = result.indexOf("filename");
                    if (location >= 0) {
                        result = result.substring(location
                                + "filename".length());
                        filename = result
                                .substring(result.indexOf("=") + 1);
                        isok = true;
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }// ISO-8859-1 UTF-8 gb2312
            }
            if (isok) {
                break;
            }
        }
        // 从路径中获取
/*        if (filename == null || "".equals(filename)) {
            filename = url.substring(url.lastIndexOf("/") + 1);
        }*/
        return filename;

    }

    public void getConnection(String method)
    {
        try {
            conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod(method);
            conn.setConnectTimeout(3000);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setInstanceFollowRedirects(true);
            conn.setRequestProperty("connection", "keep-alive");
            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
//            conn.setRequestProperty("accept","application/json");
            conn.connect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendMessage(String jsonStr)
    {
        //向url发送数据
        try {
            DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream());
            outputStream.writeBytes(jsonStr);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendMessage()
    {
        //向url发送数据
        try {
            DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream());
//            outputStream.writeBytes(null);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getResponse()
    {

        StringBuffer sb = new StringBuffer("");
        //获取url返回的数据
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String lines = null;
            while ((lines = reader.readLine()) != null) {
                lines = URLDecoder.decode(lines, "utf-8");
                sb.append(lines);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public void closeConnection()
    {
        // 断开连接
        conn.disconnect();
    }







}
